<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2019/6/3
 * Time: 19:18
 */

namespace app\Module\Index\Controller;


use app\Module\Index\Model\CirclePhotoModel;
use app\Module\Index\Model\CirclePhotoTypeModel;
use app\Module\Index\Model\UserModel;

class CirclePhotoTypeController extends CheckAuth
{
    /**
     * 获取圈子类型列表
     *
     * @verify int|null $circle_id
     * @return string
     */
    public function getListAction($circle_id)
    {
        if (empty($circle_id)) {
            $userModel = new UserModel();
            $userData  = $userModel->row(['id' => session('userId')], 600);
            $circle_id = $userData['circle_id'];
            if (empty($circle_id)) {
                return fail('未加入圈子');
            }
        }

        $circlePhotoTypeModel = new CirclePhotoTypeModel();
        $circlePhotoTypeModel->addWhere([
            'circle_id' => $circle_id,
            'is_hide'   => 0,
        ]);
        $circlePhotoTypeModel->addOrder('created', false);
        $data = $circlePhotoTypeModel->select();

        return success($data);
    }

    /**
     * 添加圈子相册
     *
     * @verify string $name
     * @return string
     */
    public function addAction($name)
    {
        $userModel = new UserModel();
        $userData  = $userModel->row(['id' => session('userId')], 600);
        $circle_id = $userData['circle_id'];
        if (empty($circle_id)) {
            return fail('未加入圈子');
        }

        $circlePhotoTypeModel = new CirclePhotoTypeModel();
        $circlePhotoTypeModel->addWhere([
            'circle_id' => $circle_id,
            'name'      => $name,
            'is_hide'   => 0,
        ]);
        $data = $circlePhotoTypeModel->row();
        if (!empty($data)) {
            return fail('该相册已存在');
        } else {
            $id = $circlePhotoTypeModel->insert([
                'circle_id' => $circle_id,
                'name'      => $name,
                'created'   => now(),
                'updated'   => now(),
            ]);
            return success($id);
        }
    }

    /**
     * 修改圈子相册
     *
     * @verify int $id
     * @verify string $name
     * @return string
     */
    public function updateAction($id, $name)
    {
        $userModel = new UserModel();
        $userData  = $userModel->row(['id' => session('userId')], 600);
        $circle_id = $userData['circle_id'];
        if (empty($circle_id)) {
            return fail('未加入圈子');
        }

        $circlePhotoTypeModel = new CirclePhotoTypeModel();
        $circlePhotoTypeModel->addWhere([
            'circle_id' => $circle_id,
            'name'      => $name,
        ]);
        $data = $circlePhotoTypeModel->row();
        if (!empty($data)) {
            return fail('该相册已存在');
        } else {
            $circlePhotoTypeModel->addWhere([
                'circle_id' => $circle_id,
                'id'        => $id,
            ]);
            $circlePhotoTypeModel->update([
                'name'    => $name,
                'updated' => now(),
            ]);
            return success(true);
        }
    }

    /**
     * 删除圈子相册
     *
     * @verify int $id
     * @return string
     */
    public function deleteAction($id)
    {
        $userModel = new UserModel();
        $userData  = $userModel->row(['id' => session('userId')], 600);
        $circle_id = $userData['circle_id'];
        if (empty($circle_id)) {
            return fail('未加入圈子');
        }

        $circlePhotoTypeModel = new CirclePhotoTypeModel();
        $circlePhotoTypeModel->addWhere([
            'circle_id' => $circle_id,
            'id'        => $id,
        ]);
        $circlePhotoTypeModel->update(['is_hide' => 1]);

        $circlePhotoModel = new CirclePhotoModel();
        $circlePhotoModel->update([
            'is_hide' => 1,
        ], [
            'circle_id'            => $circle_id,
            'circle_photo_type_id' => $id,
        ]);

        return success(true);
    }
}